package com.tianheng.wanzhou.datacollecting.util;

import java.math.BigInteger;
import java.text.DecimalFormat;

/**
 * @Author: yujw
 * @Date: 2019/11/16 8:36
 * @Version 1.0
 */
public class FloatOperate {
    public static Data operate(String strHex) {
        BigInteger b = new BigInteger(strHex, 16);
        float f = Float.intBitsToFloat(b.intValue());
        DecimalFormat df = new DecimalFormat("0.00");
        return new Data(df.format(f));
    }
// 正常16转float
    public static Data decimal1(String strHex) {
        BigInteger b = new BigInteger(strHex, 16);
        float f = Float.intBitsToFloat(b.intValue());
        DecimalFormat df = new DecimalFormat("0.0");
        return new Data(df.format(f));
    }
//反转16转float(前低4位，后高4位)
    public static Float decimal(String strHex) {
        String low = strHex.substring(0, 4);
        String high = strHex.substring(4);
        String hex = high + low;
        BigInteger b = new BigInteger(hex, 16);
        Float f = Float.intBitsToFloat(b.intValue());
        DecimalFormat df = new DecimalFormat("0.0");
        df.format(f);
        return f;
    }
    //正常16转float
    public static Float decima2(String strHex){
        BigInteger b = new BigInteger(strHex, 16);
        Float f = Float.intBitsToFloat(b.intValue());
        DecimalFormat df = new DecimalFormat("0.0");
        df.format(f);
        return f;
    }
    //反转16转double(前低4位，后高4位)
    public static Double decima3(String strHex){
        Double b = Double.parseDouble(String.valueOf(decimal(strHex)));
        return  b;
    }
    //正常16转double
    public static Double decima4(String strHex){
        Double b = Double.parseDouble(String.valueOf(decima2(strHex)));
        return  b;
    }

}
